package com.future.mayi.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.app.App;
import com.future.mayi.jpush.JPushManager;

public class StartActivity extends Activity {
    static Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if (!TextUtils.isEmpty(App.getUid()))
            new JPushManager().setAlias(App.getUid());
        openApp();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent;
                intent = new Intent(StartActivity.this, MainActivity.class);
                String extraMsg = getIntent().getStringExtra(JPushManager.EXTRA_EXTRA);
                if (!TextUtils.isEmpty(extraMsg)) {
                    intent.putExtra(JPushManager.EXTRA_EXTRA, extraMsg);
                }
                startActivity(intent);
                finish();
            }
        }, 1000);

    }



    private void openApp() {
        String token = App.getToken(this);
        if (TextUtils.isEmpty(token)) return;
        new HttpRequest<DefaultResponse>()
                .with(this)
                .setApiCode(ApiCst.openapp)
                .addParam("token", token)
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                    }
                }).start(new DefaultResponse());
    }



    @Override
    protected void onDestroy() {
        if (handler != null)
            handler.removeCallbacksAndMessages(null);
        super.onDestroy();
    }
}
